var container;

$(document).ready(function() {
	// create containers
	$(document.body).append('<div id="myTouch1" class="jsTouchPanel" style="position: absolute; left: 0px; top: 0px; border-left: 0px !important;"></div>');
	// init boxes
	container = jsTouch.init('myTouch1', {
		width : 320,
		page : 'pages/home.html'
	});
	app.container = container
	resize();
	if (app.isWindowOS()) {
		onDeviceReady();
	}

});
// events document.addEventListener('touchmove', function (e) { e.preventDefault(); }, false); // prevent default scroll
document.addEventListener('orientationchange', resize, false);
window.addEventListener('resize', resize, false);
document.addEventListener("deviceready", onDeviceReady, false);

function resize() {
	var width = parseInt(window.innerWidth);
	var height = parseInt(window.innerHeight);
	container.width = width;
	jsTouch.resize();
}

function onDeviceReady() {
	document.addEventListener("backbutton", onBackKeyDown, false);
	var db = app.getDB();
	db.transaction(populateDB, errorCB, successCB);
}

function populateDB(tx) {
	tx.executeSql('create table if not exists  category (id integer primary key autoincrement not null, name varchar(10), sort smallint)');
	tx.executeSql('create table if not exists  menu (id integer primary key autoincrement not null, name varchar(20), unit varchar, unit_price float, category_id integer)');
	tx.executeSql('create table if not exists dinner_table(id integer primary key autoincrement not null ,name varchar(30), sort smallint)');
	tx.executeSql("create table if not exists bill (id integer primary key autoincrement not null, table_name varchar(10), total_price float, send_by varchar(10), send_time datetime, print_status smallint(1), print_time datetime)");
	tx.executeSql("create table if not exists bill_menu (id integer primary key autoincrement not null, name varchar(20), unit varchar(5), unit_price float, count float, menu_id integer, bill_id integer)");
	tx.executeSql("create table if not exists  setting (id integer primary key autoincrement not null, code varchar(20), name varchar(20), value varchar(30), sort smallint)");
}

function errorCB(tx, err) {
	alert("err code:" + err);
	console.log(err);
}

function successCB() {
}

function onBackKeyDown(){
	app.msg.comfirm("提示","是否要退出此系统?",function(){
		navigator.app.exitApp();  
		console.log("退出")
	});
}
